草庐IT

c++ - Clang、std::next、libstdc++ 和 constexpr-ness

全部标签

c++ - 使用 SFINAE 检测 C++ 中类型的 POD-ness

这里的原标题是解决VS2005C++中SFINAE错误的方法这是尝试性地使用SFINAE来为TR1中存在的is_pod模板类创建等效项(在VS2005中还没有TR1)。当模板参数是POD类型(包括基本类型和由它们构成的结构)时,它的value成员应该是true,如果不是(就像非平凡的构造函数),它应该是false。templateclassis_pod{public:typedefcharYes;typedefstruct{chara[2];}No;templatestaticYestest(int){union{TvalidPodType;}u;}templatestaticNote

c++ - 为什么我不能将此对象推送到我的 std::list 中?

刚开始用C++编程。我创建了一个Point类、一个std::list和一个迭代器,如下所示:classPoint{public:intx,y;Point(intx1,inty1){x=x1;y=y1;}};std::listpointList;std::list::iteratoriter;然后我将新点推送到pointList。现在,我需要遍历pointList中的所有点,所以我需要使用迭代器进行循环。这就是我搞砸的地方。for(iter=pointList.begin();iter!=pointList.end();iter++){PointcurrentPoint=*iter;gl

C++ 自动类型转换为 std::string 和 char* 的区别

作为学习练习,我一直在研究C++中的自动类型转换是如何工作的。我知道通常应该避免自动类型转换,但我还是想通过了解它的工作原理来增加我对C++的了解。我已经创建了一个可以自动转换为std::string的StdStringConverter类,但是编译器(Debian上的g++4.3.4)似乎没有这样做将对象与真实的std::string进行比较时的转换(请忽略缺少按引用传递和不必要地创建临时对象的情况):#includeclassStdStringConverter{public:explicitStdStringConverter(std::stringname):m_name(na

C++,重载 std::swap,编译器错误,VS 2010

我想在我的模板类中重载std::swap。在下面的代码中(简化)#ifndefPoint2D_H#definePoint2D_HtemplateclassPoint2D{protected:Tx;Ty;public:Point2D():x(0),y(0){}Point2D(constT&x_,constT&y_):x(x_),y(y_){}....public:voidswap(Point2D&p);};templateinlinevoidswap(Point2D&p1,Point2D&p2){p1.swap(p2);}namespacestd{templateinlinevoidsw

c++ - std::vector 的 Typedef 和 ostream 运算符

我创建了一个Chromosome类,它最终只是一个带有ostream运算符的vector包装器,所以我决定改用typedefvector。但是,我在使用模板化的ostream运算符时遇到了问题……这是最好的方法吗?(我见过一些方法,但都没有奏效)templateclassChromosome{public:typedeftypenamestd::vectortype;typedeftypenamestd::pairptr_pair;};template//line19below:std::ostream&operator::type&chromosome){for(autoiter=c

c++ - 复制 std::ofstream 追加内容

我正在使用std::ofstream进行跟踪输出。出于某些原因,有时我想将附加在std::ofstream末尾(尚未刷新或关闭)的内容复制到另一个std::ofstream中;您有什么办法可以实现吗?谢谢 最佳答案 Tee从Boost.Iostreams过滤可以将输出流分成两部分。这是一个深受JohannesSchaub给出的启发的例子在他的回答中here.#include#include#include#includeintmain(){namespaceio=boost::iostreams;typedefio::tee_dev

c++ - solaris (x86) 上std::basic_string 的一些疑惑

solaris(x86)上std::basic_string的一些困惑#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws(s,s+6);for(inti=0;i运行结果为:9799101000为什么不是979899100101102代码#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws;ws.resize(6);for(inti=0;i可以得到预期的结果。我使用gcc3.4.6,构建命令是g++-fshort-wcharstri

c++ - 当代码不严格符合标准时,如何强制 clang 抛出错误?

我正在编写一个C++11库,我希望它能与支持C++11的每个编译器一起使用。我不想不小心编写不标准的代码(例如使用VLA或block等语言扩展)。如何强制clang禁用所有语言扩展,以便我只能编写一致的代码? 最佳答案 尝试oneofthese:-pedantic:Warnonlanguageextensions.-pedantic-errors:Erroronlanguageextensions. 关于c++-当代码不严格符合标准时,如何强制clang抛出错误?,我们在StackOve

c++ - std::bad_alloc 之后 std::vector 的状态

我试图找到一个在线引用来查看几个标准容器的异常安全性。在std::vector的情况下,它是否保持push_back调用之前的状态?我假设vector的所有对象仍然有效(没有调用析构函数)。在push_back抛出std::bad_alloc异常后,提供什么保证std::vector? 最佳答案 如果它抛出,vector不会改变。甚至不是capacity()。根据[container.requirements.general]:Unlessotherwisespecified(see23.2.4.1,23.2.5.1,23.3.3.

c++ - std::call_once 和内存重新排序

给定来自here的代码:classlazy_init{mutablestd::once_flagflag;mutablestd::unique_ptrdata;voiddo_init()const{data.reset(newexpensive_data);}public:expensive_dataconst&get_data()const{std::call_once(flag,&lazy_init::do_init,this);return*data;}};我在其他地方也看到了相同模式的一些变体。所以我的问题是:为什么这段代码被认为是保存的?以及为什么编译器不能在调用std::c